import { Product } from "../../models/product";
import { productUnionType } from "../actions/product.action";

export interface ProductState {
  createdAt: {
    loaded: boolean,
    success: boolean,
    products: Product[]
  },
  sold: {
    loaded: boolean,
    success: boolean,
    products: Product[]
  },
  search: Product[],
  filter: {
    loaded: boolean,
    success: boolean,
    result: {
      size: number,
      data: Product[]
    }
  },
  product: {
    loaded: boolean,
    success: boolean,
    result: Product
  }
}
const initialState: ProductState = {
  createdAt: {
    loaded: false,
    success: false,
    products: []
  },
  sold: {
    loaded: false,
    success: false,
    products: []
  },
  search: [],
  filter: {
    loaded: false,
    success: false,
    result: {
      size: 0,
      data: []
    }
  },
  product: {
    loaded: false,
    success: false,
    result: {
      _id: "",
      name: "",
      price: 0,
      description: "",
      category: {
        _id: "",
        name: ""
      },
      quantity: 0,
      sold: 0,
      photo: new FormData(),
      shipping: false,
      createdAt: ""
    }
  }
}
export default function productReducer(state=initialState, action: productUnionType) {
  switch(action.type) {
    case "GET_PRODUCT": 
      return {
        ...state,
        [action.sortBy]: {
          ...state[action.sortBy==="createdAt"? "createdAt":"sold"],
          loaded: false,
          success: false,
        }
      };
    case "GET_PRODUCT_SUCCESS": 
      return {
        ...state,
        [action.sortBy]: {
          loaded: false,
          success: false,
          products: action.payload
        }
      };
    case "SEARCH_PRODUCT_SUCCESS":
      return {
        ...state,
        search: action.products
      };
    case "FILTER_PRODUCT":
      return {
        ...state,
        filter: {
          loaded: false,
          success: false,
          result: {
            size: 0,
            data: [...state.filter.result.data]
          }
        }
      };
    case "FILTER_PRODUCT_SUCCESS":
      let data = action.skip === 0 ? action.payload.data:
                                        [...state.filter.result.data, ...action.payload.data]
      return {
        ...state,
        filter:{
          loaded:true,
          success:true,
          result: {
            size: action.payload.size,
            data
          }
        }
      }
    case "GET_PRODUCTBY_ID":
      return {
        ...state,
        product: {
          ...state.product,
          loaded: false,
          success: false,
        }
      }
    case "GET_PRODUCTBY_ID_SUCCESS":
      return {
        ...state,
        product: {
          loaded: true,
          success: true,
          result: action.payload
        }
      }
    default:
      return state;
  }

}